﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI;
using Windows.UI.Xaml.Data;
using Windows.UI.Xaml.Media;

namespace Windows8Accelerator.Core.Xaml.Controls
{
    public class BackgroundColorToBorderColorConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, string language)
        {
            if (value is SolidColorBrush && targetType.Equals(typeof(Brush)))
            {
                var color = ((SolidColorBrush)value).Color;
                return new SolidColorBrush(
                    Color.FromArgb(
                    color.A,
                    DoubleMultiple(color.R, .2),
                    DoubleMultiple(color.G, .2),
                    DoubleMultiple(color.B, .2)));
            }
            if (value is Brush)
                return value;
            return System.Convert.ChangeType(value, targetType);
        }

        private byte DoubleMultiple(double p1, double p2)
        {
            return System.Convert.ToByte(p1 * p2);
        }

        public object ConvertBack(object value, Type targetType, object parameter, string language)
        {
            return System.Convert.ChangeType(value, targetType);
        }
    }
}
